package com.star.carservice.client.fragment;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.database.CursorJoiner.Result;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.star.carservice.R;
import com.star.carservice.client.activity.SelectCarActivity;
import com.star.carservice.client.adapter.CarBrandListAdapter;
import com.star.carservice.client.adapter.CarModelListAdapter;
import com.star.carservice.client.adapter.CarSeriesListAdapter;
import com.star.carservice.client.fragment.base.BaseFragment;
import com.star.carserviceapi.domain.CarFeatureInfo;
import com.star.carserviceapi.domain.request.QryCarBrandRequest;
import com.star.carserviceapi.domain.request.QryCarModelRequest;
import com.star.carserviceapi.domain.request.QryCarSeriesRequest;
import com.star.carserviceapi.domain.request.QryCarYearRequest;
import com.star.carserviceapi.domain.result.QryCarBrandResult;
import com.star.carserviceapi.domain.result.QryCarModelResult;
import com.star.carserviceapi.domain.result.QryCarSeriesResult;
import com.star.carserviceapi.domain.result.QryCarYearResult;

public class SelectCarModelFragment extends BaseFragment {

	private final static String TAG = SelectCarModelFragment.class
			.getSimpleName();



	public static SelectCarModelFragment getInstance(CarFeatureInfo info) {
		SelectCarModelFragment f = new SelectCarModelFragment();

		Bundle bundle = new Bundle();
		bundle.putSerializable(SelectCarActivity.KEY_CAR, info);

		f.setArguments(bundle);

		return f;
	}

	@Override
	public String getLogTag() {
		return TAG;
	}

	@InjectView(R.id.list_car_brand)
	ListView list_car_brand;
	List<CarFeatureInfo> mData = new ArrayList<CarFeatureInfo>();
	CarModelListAdapter mAdapter = null;

	CarFeatureInfo mCarFeatureInfo = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mCarFeatureInfo = (CarFeatureInfo) getArguments().getSerializable(
				SelectCarActivity.KEY_CAR);

		execAsyncTask(new QryCarModelTask(), new QryCarModelRequest(
				mCarFeatureInfo.getBrand(), mCarFeatureInfo.getSeries()));
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		if (CacheViewIsNull()) {
			mCacheView = inflater.inflate(R.layout.frag_select_car_model,
					container, false);

			ButterKnife.inject(this, mCacheView);

			initView();
		}

		removeCacheView();

		return mCacheView;
	}

	private void initView() {
		mAdapter = new CarModelListAdapter(getActivity(), mData);
		list_car_brand.setAdapter(mAdapter);

		list_car_brand.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				CarFeatureInfo item = mData.get(position);

				mCarFeatureInfo.setModel(item.getModel());

				SelectCarActivity parent = (SelectCarActivity) getActivity();

				parent.onSelectCar(mCarFeatureInfo);
			}
		});
	}

	class QryCarModelTask extends
			AsyncTask<QryCarModelRequest, Void, QryCarModelResult> {

		@Override
		protected QryCarModelResult doInBackground(QryCarModelRequest... params) {

			return getApi().qryCarModel(params[0]);
		}

		@Override
		protected void onPostExecute(QryCarModelResult result) {
			mData.clear();
			mData.addAll(result.getCars());
			mAdapter.notifyDataSetChanged();
		}
	}

}
